using System;
using System.Runtime.CompilerServices;
using System.Security.Permissions;
using System.Web;

namespace PickGold.Charting
{
	[SRDescription("DescriptionAttributeAnnotations3")]
	[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class AnnotationCollection : ChartNamedElementCollection<Annotation>
	{
		internal AnnotationCollection(IChartElement parent)
			: base(parent)
		{
		}

		internal void ChartAreaNameReferenceChanged(object sender, NameReferenceChangedEventArgs e)
		{
			if (e.OldElement == null)
				return;

			foreach (var annotation in this)
			{
				if (annotation.ClipToChartArea == e.OldName)
					annotation.ClipToChartArea = e.NewName;
				var group = annotation as AnnotationGroup;
				if (group != null)
					group.Annotations.ChartAreaNameReferenceChanged(sender, e);
			}
		}

		internal override void Deinitialize(Annotation item)
		{
			if (item != null)
			{
				item.annotationGroup = null;
				item.ResetCurrentRelativePosition();
			}
			base.Deinitialize(item);
		}

		public override Annotation FindByName(string name)
		{
			foreach (var annotation in this)
			{
				if (annotation.Name == name)
					return annotation;

				var group = annotation as AnnotationGroup;
				if (group != null)
				{
					var annotation2 = group.Annotations.FindByName(name);
					if (annotation2 != null)
						return annotation2;
				}
			}
			return null;
		}

		internal override void Initialize(Annotation item)
		{
			if (item != null)
			{
				var annotation = item as TextAnnotation;
				if (((annotation != null) && string.IsNullOrEmpty(annotation.Text)) && ((base.Chart != null) && base.Chart.IsDesignMode()))
					annotation.Text = item.Name;
				if (this.AnnotationGroup != null)
					item.annotationGroup = this.AnnotationGroup;
				item.ResetCurrentRelativePosition();
			}
			base.Initialize(item);
		}

		internal void Paint(ChartGraphics chartGraph, bool drawAnnotationOnly)
		{
			foreach (var annotation in this)
			{
				annotation.ResetCurrentRelativePosition();
				if (annotation.IsVisible())
				{
					bool flag = false;
					if (annotation.IsAnchorVisible())
					{
						if (((annotation.ClipToChartArea.Length > 0) && (annotation.ClipToChartArea != "NotSet")) && (base.Chart != null))
						{
							int index = base.Chart.ChartAreas.IndexOf(annotation.ClipToChartArea);
							if (index >= 0)
							{
								var area = base.Chart.ChartAreas[index];
								chartGraph.SetClip(area.PlotAreaPosition.ToRectangleF());
								flag = true;
							}
						}
						string strOriginal = string.Empty;
						strOriginal = annotation.Url;
						chartGraph.StartHotRegion(annotation.ReplaceKeywords(strOriginal), annotation.ReplaceKeywords(annotation.ToolTip));
						annotation.Paint(base.Chart, chartGraph);
						chartGraph.EndHotRegion();
						if (flag)
						{
							chartGraph.ResetClip();
						}
					}
				}
			}
		}

		internal AnnotationGroup AnnotationGroup { set; get; }
	}
}

